package com.greenx.core.common.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public final class DateUtils {

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(getCurrentXMLGregorianCalendar());

	}

	/**
	 * 
	 */
	public static void getCurrentDate() {

		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		// get current date time with Date()
		Date date = new Date();
		System.out.println(dateFormat.format(date));

		// get current date time with Calendar()
		Calendar cal = Calendar.getInstance();
		System.out.println(dateFormat.format(cal.getTime()));

		System.out.println(date.toString());
	}

	/**
	 * get current date by XMLGregorianCalendar
	 * 
	 * @return current date by XMLGregorianCalendar
	 */
	public static final XMLGregorianCalendar getCurrentXMLGregorianCalendar() {
		return getXMLGregorianCalendar(new Date());
	}

	/**
	 * translate date to XMLGregorianCalendar
	 * 
	 * @param date
	 *            one day
	 * @return the date has been convert to XMLGregorianCalendar
	 */
	public static final XMLGregorianCalendar getXMLGregorianCalendar(Date date) {
		// new instance of DatatypeFactory
		DatatypeFactory dataTypeFactory;
		try {
			dataTypeFactory = DatatypeFactory.newInstance();
		} catch (DatatypeConfigurationException e) {
			throw new RuntimeException(e);
		}
		// new instance of GregorianCalendar
		GregorianCalendar gc = new GregorianCalendar();
		// set time to GregorianCalendar
		gc.setTimeInMillis(date.getTime());
		// get XMLGregorianCalendar
		return dataTypeFactory.newXMLGregorianCalendar(gc);
	}

}
